#include <cstdio>
#include <iostream>
#include <cstring>
#include <map>
#define LL long long
#define int long long
#define IN inline
#define re register
using namespace std;
IN int read() {
	int res = 0; char ch = getchar();
	for(; !isdigit(ch); ch = getchar());
	for(; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ 48);
	return res;
}
int n;
char ch[10000], s[1000];
map<string, int> f;
int l, num[100], cnt, flag, flag1[1000];
bool work(int x) {return x > 255;}
bool check() {
//	puts(s + 1);
	l = strlen(s + 1), num[100], cnt = 0, flag = 0;
	num[0] = num[1] = num[2] = num[3] = num[4] = 0;
	for(int i = 0; i <= 4; ++i) flag1[i] = 0;
	for(int i = 1; i <= l; ++i) {
		if(s[i] == '.') cnt ++;
		else if(isdigit(s[i])) {
			num[cnt] = num[cnt] * 10 + s[i] - '0';
			if(cnt <= 3 && work(num[cnt])) return 0;
			if(cnt == 4 && num[cnt] > 65535) return 0;
			if(flag1[cnt] == 1 && num[cnt] > 0) return 0;
			if(num[cnt] == 0) flag1[cnt]++;
		}
		else if(s[i] == ':') {
			if(cnt != 3) return 0;
			flag++, cnt ++;
		}
		else return 0;
		if(!isdigit(s[i]) && !isdigit(s[i - 1])) return 0;
	}
//	printf("%d %d %d %d %d %d %d\n",cnt,flag,num[0],num[1],num[2],num[3],num[4]);
	if(cnt != 4 || flag != 1) return 0;
//	if(work(num[0]) || work(num[1]) || work(num[2]) || work(num[3]) || (num[4] > 65535)) return 0;
	for(int i = 0; i <= 4; ++i)
		if(flag1[i] > 1) return 0;
	return 1;
}
signed main() {
	freopen("network4.in","r",stdin);
	freopen("network4.out","w",stdout);
	n = read();
	for(int i = 1; i <= n; ++i) {
//		memset(s, 0, sizeof(s));
		scanf("%s%s",ch + 1,s + 1);
		if(!check()) {
			printf("ERR\n");
			continue;
		}
		if(ch[1] == 'S'){
			if(f[s + 1]) {
				printf("FAIL\n");
				continue;
			}
			else {
				printf("OK\n");
				f[s + 1] = i;
				continue;
			}
		}
		else {
			if(f[s + 1] == 0) {
				printf("FAIL\n");
				continue;
			}
			else {
				printf("%d\n",f[s + 1]);
				continue;
			}
		}
	}
	return 0;
}
